//
//  ForgetPassFirstStepTableViewController.swift
//  ZNHXManagement
//
//  Created by 四川中农 on 2018/9/28.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class ForgetPassFirstStepTableViewController: BaseTableViewController {
    
    @IBOutlet weak var phoneField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        self.title = "忘记密码"
        self.phoneField.addTarget(self, action: #selector(valueChange(textField:)), for: .editingChanged)
        self.phoneField.setValue(UIColor.init(hexColor: "#cccccc"), forKeyPath: "_placeholderLabel.textColor")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    @objc func valueChange(textField:UITextField){
        let maxLength = 11
        let toBeString = textField.text!
        let lang = textField.textInputMode?.primaryLanguage
        if lang == "zh-Hans" {
            let selectedRange = textField.markedTextRange
            if selectedRange == nil && toBeString.count > maxLength{
                let endIndex = toBeString.index(toBeString.startIndex, offsetBy: maxLength)
                textField.text = String(toBeString[..<endIndex])
            }
        }else if toBeString.count > maxLength {
            let endIndex = toBeString.index(toBeString.startIndex, offsetBy: maxLength)
            textField.text = String(toBeString[..<endIndex])
        }
    }
    
    // MARK:下一步
    @IBAction func nextButtonClick(_ sender: UIButton) {
        
        PKhud.show()
        HttpRequest.requestData(.post, URLString: Api.get_code.rawValue, parameters: ["username":self.phoneField.text!]) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                self.view.endEditing(true)
                let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "ForgetPassWordSecondStepTableViewController") as! ForgetPassWordSecondStepTableViewController
                ctrl.phone = self.phoneField.text!
                self.navigationController?.pushViewController(ctrl, animated: true)
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
 

}
